# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :model.py
# @Time      :2022/6/28 14:02
# @Author    :吕华龙
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

db = SQLAlchemy()


# 用户模型类
class User(db.Model):
    uid = db.Column(db.Integer, primary_key=True, autoincrement=True, comment="用户ID")
    mobile = db.Column(db.String(11), nullable=False, default="", comment="手机号")
    password = db.Column(db.String(128), nullable=False, default="", comment="密码")
    user_name = db.Column(db.String(32), nullable=False, default="", unique=True, comment="用户名")
    img = db.Column(db.String(128), nullable=False, default="", comment="头像")
    is_media = db.Column(db.Boolean, nullable=False, default=False, comment="是否为自媒体账号")
    intro = db.Column(db.String(256), nullable=False, default="这个人很懒，什么都没有留下", comment="简介")
    article_count = db.Column(db.Integer, nullable=False, default=0, comment="发帖数")
    following_count = db.Column(db.Integer, nullable=False, default=0, comment="关注数")
    fans_count = db.Column(db.Integer, nullable=False, default=0, comment="粉丝数")
    like_count = db.Column(db.Integer, nullable=False, default=0, comment="累计点赞数")
    read_count = db.Column(db.Integer, nullable=False, default=0, comment="累计阅读数")
    status = db.Column(db.Boolean, nullable=False, default=True, comment="是否可用")
    last_login = db.Column(db.DateTime, nullable=False, default=datetime.now, comment="最后登录时间")
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now, comment="注册时间")
    news = db.relationship("News", backref="user")
    comment = db.relationship("Comment", backref="user")
    collect = db.relationship("Collect", backref="user")
    digg = db.relationship("Digg", backref="user")


# 用户关注表
class Follow(db.Model):
    fid = db.Column(db.Integer, primary_key=True, autoincrement=True, comment="关注ID")
    user_id = db.Column(db.Integer, db.ForeignKey("user.uid"), primary_key=True, comment="关注者ID")
    flooer_id = db.Column(db.Integer, db.ForeignKey("user.uid"), primary_key=True, comment="被关注者ID")
    status = db.Column(db.Boolean, nullable=False, default=True, comment="状态(1,关注;0, 取消)")
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now, comment="创建时间")
    update_time = db.Column(db.DateTime, nullable=False, default=datetime.now, comment="更新时间")


# 用户频道表  多对多的表
user_channel = db.Table(
    "user_channel",
    db.Column("uc_id", db.Integer, primary_key=True, autoincrement=True, comment="用户频道ID"),
    db.Column("user_id", db.Integer, db.ForeignKey("user.uid"), primary_key=True, comment="用户ID"),
    db.Column("channel_id", db.Integer, db.ForeignKey("channel.cid"), primary_key=True, comment="频道ID"),
)


# 频道表
class Channel(db.Model):
    cid = db.Column(db.Integer, primary_key=True, autoincrement=True, comment="频道ID")
    name = db.Column(db.String(16), nullable=False, default="", comment="频道名称")
    is_show = db.Column(db.Boolean, nullable=False, default=True, comment="是否课件(1,可见;0,不可见)")
    score = db.Column(db.Integer, nullable=False, default=0, comment="排序，越大越靠前")
    user = db.relationship("User", secondary=user_channel, backref=db.backref("channels"))
    news = db.relationship("News", backref="channel")


# 咨询表
class News(db.Model):
    nid = db.Column(db.Integer, primary_key=True, autoincrement=True, comment="文章ID")
    user_id = db.Column(db.Integer, db.ForeignKey("user.uid"), comment="用户ID")
    channel_id = db.Column(db.Integer, db.ForeignKey("channel.cid"), comment="频道ID")
    title = db.Column(db.String(64), nullable=False, default="", comment="标题")
    content = db.Column(db.Text, nullable=False, default="", comment="帖文内容")
    img = db.Column(db.String(128), nullable=False, default="", comment="封面")
    comment_count = db.Column(db.Integer, nullable=False, default=0, comment="评论数")
    digg_count = db.Column(db.Integer, nullable=False, default=0, comment="点赞数")
    read_count = db.Column(db.Integer, nullable=False, default=0, comment="阅读数")
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now, comment="创建时间")
    update_time = db.Column(db.DateTime, nullable=False, default=datetime.now, comment="更新时间")
    comment = db.relationship("Comment", backref="news")
    collect = db.relationship("Collect", backref="news")
    digg = db.relationship("Digg", backref="news")


# 评论表
class Comment(db.Model):
    cmid = db.Column(db.Integer, primary_key=True, autoincrement=True, comment="评论ID")
    user_id = db.Column(db.Integer, db.ForeignKey("user.uid"), comment="用户ID")
    news_id = db.Column(db.Integer, db.ForeignKey("news.nid"), comment="文章ID")
    parent_id = db.Column(db.Integer, nullable=False, default=0, comment="被评论的评论id")
    digg_count = db.Column(db.Integer, nullable=False, default=0, comment="点赞数")
    reply_count = db.Column(db.Integer, nullable=False, default=0, comment="回复数")
    content = db.Column(db.String(128), nullable=False, default="", comment="评论内容")
    is_top = db.Column(db.Boolean, nullable=False, default=False, comment="是否置顶(1,置顶;0,不置顶)")
    status = db.Column(db.Boolean, nullable=False, default=False, comment="是否删除(1,删除;0,不删除)")
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now, comment="创建时间")


# 收藏表
class Collect(db.Model):
    collect_id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment="收藏ID")
    user_id = db.Column(db.Integer, db.ForeignKey("user.uid"), comment="用户ID")
    news_id = db.Column(db.Integer, db.ForeignKey("news.nid"), comment="文章ID")
    is_delete = db.Column(db.Boolean, nullable=False, default=False, comment="是否删除(1,删除;0,不删除)")


# 收藏表
class Digg(db.Model):
    digg_id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment="点赞ID")
    user_id = db.Column(db.Integer, db.ForeignKey("user.uid"), comment="用户ID")
    news_id = db.Column(db.Integer, db.ForeignKey("news.nid"), comment="文章ID")
    is_delete = db.Column(db.Boolean, nullable=False, default=False, comment="是否删除(1,删除;0,不删除)")


# 轮播图
class Imgs(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment="轮播图ID")
    title = db.Column(db.String(128), nullable=False, default="", comment="轮播图标题")
    path = db.Column(db.String(128), nullable=False, default="", comment="轮播图内容")
    is_show = db.Column(db.Boolean, nullable=False, default=True, comment="是否显示(1,显示;0,不显示)")
